package cn.iocoder.yudao.module.infra.dal.mysql.codeincrement;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.infra.controller.admin.codeincrement.vo.CodeIncrementPageReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.codeincrement.CodeIncrementDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

/**
 * code自增号 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface CodeIncrementMapper extends BaseMapperX<CodeIncrementDO> {

    default PageResult<CodeIncrementDO> selectPage(CodeIncrementPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CodeIncrementDO>()
                .eqIfPresent(CodeIncrementDO::getBizKey, reqVO.getBizKey())
                .eqIfPresent(CodeIncrementDO::getInrValue, reqVO.getInrValue())
                .eqIfPresent(CodeIncrementDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(CodeIncrementDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(CodeIncrementDO::getUpdater, reqVO.getUpdater())
                .orderByDesc(CodeIncrementDO::getId));
    }

    @SelectKey(keyProperty = "inrValue",before = false,resultType =Integer.class,keyColumn = "inr_value",
            statement = {"select inr_value from infra_code_increment where biz_key = #{bizKey}"})
    @Update("update infra_code_increment set inr_value=inr_value + 1  where biz_key = #{bizKey} ")
    void updateSpuAndSkuCode(CodeIncrementDO codeIncrementDOs);

}